//给定一个排序链表，删除所有含有重复数字的节点，只保留原始链表中 没有重复出现 的数字。 
//
// 示例 1: 
//
// 输入: 1->2->3->3->4->4->5
//输出: 1->2->5
// 
//
// 示例 2: 
//
// 输入: 1->1->1->2->3
//输出: 2->3 
// Related Topics 链表 
// 👍 382 👎 0

package leetcode.editor.cn;

import common.bean.ListNode;
import common.util.MyUtil;

/**
 * Java：删除排序链表中的重复元素 II
 *
 * @author changgui
 */
@SuppressWarnings("all")
public class P82_RemoveDuplicatesFromSortedListIi {
    public static void main(String[] args) {
        Solution solution = new P82_RemoveDuplicatesFromSortedListIi().new Solution();
        MyUtil.printListNode(solution.deleteDuplicates(MyUtil.createNode(1, 1, 1, 1, 3, 4, 5)));

    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public ListNode deleteDuplicates(ListNode head) {
            ListNode preHead = new ListNode(0);
            preHead.next = head;
            boolean deleteNext = false;
            for (ListNode curr = preHead; curr != null && curr.next != null; ) {
                if (curr.next.next != null) {
                    if (curr.next.val == curr.next.next.val) {
                        deleteNext = true;
                        curr.next.next = curr.next.next.next;
                        continue;
                    }
                    if (curr.next.val != curr.next.next.val && deleteNext) {
                        curr.next = curr.next.next;
                        deleteNext = false;
                        continue;
                    }
                }
                // 解决这种 1->1->->null
                if (deleteNext) {
                    curr.next = curr.next.next;
                    continue;
                }
                // 挪动
                curr = curr.next;
            }
            return preHead.next;
        }
    }
    //leetcode submit region end(Prohibit modification and deletion)

}